package com.landis.temple.mobile;


import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

/**
 * Class takes care of application menu
 * 
 * @author John Landis 
 * @author Anton Kronaj
 *
 */

public class MyAppMenus{
    
	private static String TAG = "MyAppMenus"; 
	
	/**
	 * creates the menu that appears when the device menu button is pressed.
	 * @param menu
	 * @param a	the activity that is calling 
	 * @param menuRes	the id of the menu items. The xml library_menu.xml in the res/menu folder
	 * @return true on successful completion
	 */
	public static boolean createMenu(Menu menu, Activity a, int menuRes) {
        MenuInflater inflater = a.getMenuInflater();
        inflater.inflate( menuRes , menu);
        return true;
    }
	
	/**
	 * Handles the call when a menu button is pressed.
	 * @param item	the menu item that the user pressed
	 * @param a	the activity from which the menu item was pressed
	 * @param myId	the id of the author as it is stored in the local database
	 * @return true on successful handling of the request, false otherwise.
	 */
    public static boolean menuItemSelected(MenuItem item, Activity a, long myId) {
        // Handle item selection
    	Intent i;
        switch (item.getItemId()) {
        
        	case R.id.menu_create_story:
        		if(!TwitterBackend.isLoggedIn)
    	        {
    			 Toast.makeText(a, "Log in to create a story" , Toast.LENGTH_SHORT ).show();
    	        }
    	        else
    	        {
    	        	i = new Intent( a , CreateActivity.class );
                	i.putExtra( "action"    , TwitterLibrary.CREATE_STORY);
                	i.putExtra( "author_id" , myId          );
                	a.startActivityForResult( i , TwitterLibrary.CREATE_STORY);
    	        }
            	return true;
            	
        	case R.id.menu_logout:
            	// TODO LOGOUT
        		Toast.makeText(a, "Logout" , Toast.LENGTH_SHORT ).show();
        		TwitterBackend.logOut();
        		i = new Intent( a , TwitterLibrary.class ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        		a.startActivity(i); 
        		return true;
        	
        	case R.id.menu_view_library:
        		i = new Intent( a , TwitterLibrary.class ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        		a.startActivity(i); 
        		Toast.makeText(a, "view the current list of stories" , Toast.LENGTH_SHORT ).show();
        		return true;
        		
        	case R.id.menu_login:
        		Toast.makeText(a, "Login" , Toast.LENGTH_SHORT ).show();
        		TwitterBackend.askOAuth(a);
        		return true;
        		
        	case R.id.menu_search:
        	    i = new Intent( a , SearchActivity.class );
                i.putExtra( "author_id" , myId          );
                a.startActivity( i );
                Toast.makeText(a, "Search" , Toast.LENGTH_SHORT ).show();
                return true;
        		
        	default:
            	return false;
        }
    }
    
    
   

}
